//
//  HYMineSetManager.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/7.
//

#import "HYMineSetManager.h"
#import "HYSettingModel.h"
#import "HYHomeFolderModel.h"
#import "HYHomeVideoModel.h"
#import "HYPreviewHistoryModel.h"
#import "HYMyCollectionModel.h"
#import "HYFileDownloadModel.h"
#import "HYFileDownloadTask.h"
#import <SDWebImage/SDImageCache.h>

#define kMainLayoutType                  @"MainLayoutType"
#define kSetPrivacyTip                   @"kSetPrivacyTip"
#define kIsFristInstall                  @"kIsFristInstall"
#define kAcceptAgreement                 @"kAcceptAgreement"

static HYMineSetManager *manager = nil;

@implementation HYMineSetManager

+ (instancetype)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

- (HYLayoutType)layoutType
{
    return [kUserDefaults integerForKey:kMainLayoutType];
}

- (void)setLayoutType:(HYLayoutType)layoutType
{
    [kUserDefaults setInteger:layoutType forKey:kMainLayoutType];
    [kUserDefaults synchronize];
}

#pragma mark - getter
- (BOOL)isFirstInstall
{
    return [kUserDefaults boolForKey:kIsFristInstall];
}

- (BOOL)isAccepAgreement
{
    return [kUserDefaults boolForKey:kAcceptAgreement];
}

- (BOOL)isPrivacyTip
{
    return [kUserDefaults boolForKey:kSetPrivacyTip];
}

- (void)setIsPrivacyTip:(BOOL)isPrivacyTip
{
    [kUserDefaults setBool:YES forKey:kSetPrivacyTip];
    [kUserDefaults synchronize];
}

- (BOOL)isAllowNetwork
{
    return [kUserDefaults boolForKey:kAllowNetwork];
}

- (BOOL)isAutoPlayNext
{
    return [kUserDefaults boolForKey:kAutoPlayNext];
}

- (BOOL)isAllowBackground
{
    return [kUserDefaults boolForKey:kAllowBackground];
}

- (BOOL)isAllowPrivateBrowse
{
    return [kUserDefaults boolForKey:kAllowPrivateBrowse];
}

- (BOOL)isAllowScreenShotSavePhoto
{
    return [kUserDefaults boolForKey:kAllowScreenShotSavePhoto];
}

- (BOOL)isMutePlayback
{
    return [kUserDefaults boolForKey:kMutePlayback];
}

- (NSInteger)appThemeMode
{
    return [kUserDefaults integerForKey:kChangeTheme];
}

#pragma mark - setter
- (void)setIsFirstInstall:(BOOL)isFirstInstall
{
    [kUserDefaults setBool:isFirstInstall forKey:kIsFristInstall];
    [kUserDefaults synchronize];
}

- (void)setIsAllowNetwork:(BOOL)isAllowNetwork
{
    [kUserDefaults setBool:isAllowNetwork forKey:kAllowNetwork];
    [kUserDefaults synchronize];
}

- (void)setIsAllowPrivateBrowse:(BOOL)isAllowPrivateBrowse
{
    [kUserDefaults setBool:isAllowPrivateBrowse forKey:kAllowPrivateBrowse];
    [kUserDefaults synchronize];
    [kNotificationCenter postNotificationName:kHistroyRefreshListNotification object:nil];
}

- (void)setIsAllowScreenShotSavePhoto:(BOOL)isAllowScreenShotSavePhoto
{
    [kUserDefaults setBool:isAllowScreenShotSavePhoto forKey:kAllowScreenShotSavePhoto];
    [kUserDefaults synchronize];
}

- (void)setIsAutoPlayNext:(BOOL)isAutoPlayNext
{
    [kUserDefaults setBool:isAutoPlayNext forKey:kAutoPlayNext];
    [kUserDefaults synchronize];
}

- (void)setIsAllowBackground:(BOOL)isAllowBackground
{
    [kUserDefaults setBool:isAllowBackground forKey:kAllowBackground];
    [kUserDefaults synchronize];
}

- (void)setIsMutePlayback:(BOOL)isMutePlayback
{
    [kUserDefaults setBool:isMutePlayback forKey:kMutePlayback];
    [kUserDefaults synchronize];
}

- (void)setIsAccepAgreement:(BOOL)isAccepAgreement
{
    [kUserDefaults setBool:isAccepAgreement forKey:kAcceptAgreement];
    [kUserDefaults synchronize];
}

- (void)setAppThemeMode:(NSInteger)appThemeMode
{
    [kUserDefaults setInteger:appThemeMode forKey:kChangeTheme];
    [kUserDefaults synchronize];
    [self changeAppTheme:appThemeMode];
}

- (void)changeAppTheme:(NSInteger)appThemeMode
{
    if (@available(iOS 13.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        window.overrideUserInterfaceStyle = appThemeMode;
    }
}

+ (void)firstInstallSetInitialParameters
{
    BOOL isFirstInstall = [HYMineSetManager shareManager].isFirstInstall;
    if(!isFirstInstall){
        [HYMineSetManager shareManager].isFirstInstall = YES;
        [HYMineSetManager shareManager].isAllowNetwork = NO;
        [HYMineSetManager shareManager].isAutoPlayNext = YES;
        [HYMineSetManager shareManager].isAllowBackground = NO;
        [HYMineSetManager shareManager].isAllowScreenShotSavePhoto = YES;
        [HYMineSetManager shareManager].isMutePlayback = NO;
        [HYMineSetManager shareManager].isAccepAgreement = NO;
        [HYMineSetManager shareManager].appThemeMode = 0;
    }else{
        [[HYMineSetManager shareManager] changeAppTheme:[HYMineSetManager shareManager].appThemeMode];
    }
}

+ (void)clearAppCache
{
    //1.清除本地数据库数据
    [HYHomeFolderModel clearAllData];
    [HYHomeVideoModel clearAllData];
    [HYMyCollectionModel clearAllCollectionHistory];
    [HYPreviewHistoryModel clearAllPreviewRecord];
    [HYFileDownloadModel clearAllData];
    [HYFileDownloadTask clearAllDownloadData];
    //2.删除沙盒数据
    // libray所有缓存路径
    [kFileManager removeItemAtPath:[HYFileManager videoFileDownloadRootPath] error:nil];
    // 清空document导入文件
    [kFileManager removeItemAtPath:[HYFileManager documentDirectory] error:nil];
    //3.清除所有设置的配置
    [self clearDefatlutsValue];
    //4.SDWebImage清理缓存
    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDisk];
    //5.重启app
    [self exitApplication];
}

// 取出偏好设置的值
+ (void)clearDefatlutsValue
{
    NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
    NSDictionary *dictionary = [defatluts dictionaryRepresentation];
    for(NSString *key in [dictionary allKeys]){
        [defatluts removeObjectForKey:key];
    }
    [defatluts synchronize];
}

// 退出应用程序
+ (void)exitApplication
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [UIView animateWithDuration:0.8f animations:^{
        CGAffineTransform curent =  window.transform;
        CGAffineTransform scale = CGAffineTransformScale(curent, 0.001,0.001);
        [window setTransform:scale];
    } completion:^(BOOL finished) {
        exit(0);
    }];
}

@end
